package com.aididi;

import com.aididi.factory.Receiver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
 * @Description: 	redis消息队列
 * @Author zx.xu
 */
@SpringBootApplication
@Slf4j
public class MessageQueueApplication {

	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
												 MessageListenerAdapter listenerAdapter){
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.addMessageListener(listenerAdapter,new PatternTopic("chat"));
		return container;
	}

	@Bean
	MessageListenerAdapter listenerAdapter(Receiver receiver) {
		return new MessageListenerAdapter(receiver, "receiveMessage");
	}


	@Bean
	Receiver receiver() {
		return new Receiver();
	}

	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
		return new StringRedisTemplate(connectionFactory);
	}


	public static void main(String[] args) throws InterruptedException {
		ApplicationContext context = SpringApplication.run(MessageQueueApplication.class);
		StringRedisTemplate redisTemplate = context.getBean(StringRedisTemplate.class);
		Receiver receiver  = context.getBean(Receiver.class);
		while (receiver.getCount() == 0 ){
			log.info("Sending message..");
			redisTemplate.convertAndSend("chat","hello,send message");
			Thread.sleep(800L);
		}
		System.exit(0);

	}




}
